package com.movitech.mobile.cz.base.modules.activiti.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.movitech.mobile.cz.base.modules.activiti.dao.ext.ExtActivitiTaskMapper;

@Service("activitiTaskService")
public class ActivitiTaskService {
    protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private TaskService taskService;
	@Autowired
	private ExtActivitiTaskMapper extActivitiTaskMapper;

	/***
	 * 根据流程实例id，获取当前Task任务列表
	 * @param processInstanceId
	 * @return
	 */
	public List<Task> getTasksByProcessInstanceId(String processInstanceId){
		return taskService.createTaskQuery().processInstanceId(processInstanceId).list();
	}

	/***
	 * 根据流程实例id、任务key，获取当前Task任务列表
	 * @param processInstanceId
	 * @param taskKey
	 * @return
	 */
	public List<Task> getTasksByProcessInstanceId(String processInstanceId,String taskKey){
		return taskService.createTaskQuery().processInstanceId(processInstanceId).taskDefinitionKey(taskKey).list();
	}

	/***
	 * 根据流程中执行id，获取当前Task任务列表
	 * @param executionId
	 * @return
	 */
	public List<Task> getTasksByExecutionId(String executionId){
		return taskService.createTaskQuery().executionId(executionId).list();
	}
	
	
	/***
	 * 根据流程实例id，获取当前Task任务ids
	 * @param processInstanceId
	 * @return
	 */
	public String getTaskIdsProcessInstanceId(String processInstanceId){
		String taskIds = "";
		List<Task> lstTask = getTasksByProcessInstanceId(processInstanceId);
		if(!CollectionUtils.isEmpty(lstTask)) {
			for (Task task : lstTask) {
				if(StringUtils.isNotEmpty(taskIds)) taskIds += ",";
				taskIds += task.getId();
			}
		}
		return taskIds;
	}
	
	/***
	 * 根据流程实例id和执行id，获取当前Task任务ids
	 * @param processInstanceId
	 * @param executionId
	 * @return
	 */
	public String getTaskIdsProcessInstanceAndExecutionId(String processInstanceId,String executionId){
		String taskId = "";
		List<Task> lstTask = getTasksByProcessInstanceId(processInstanceId);
		if(!CollectionUtils.isEmpty(lstTask)) {
			for (Task task : lstTask) {
				if(StringUtils.equalsIgnoreCase(task.getExecutionId(), executionId)) {
					taskId = task.getId();
				}
			}
		}
		return taskId;
	}
	
	/***
	 * 根据流程实例id和执行id，获取当前Task任务ids
	 * @param processInstanceId
	 * @param executionId
	 * @return
	 */
	public String getTaskIdsByExecutionId(String executionId){
		String taskId = "";
		List<Task> lstTask = getTasksByExecutionId(executionId);
		if(!CollectionUtils.isEmpty(lstTask)) {
			for (Task task : lstTask) {
				if(StringUtils.isNotEmpty(taskId)) taskId += ",";
				taskId += task.getId();
			}
		}
		return taskId;
	}
	
	/***
	 * 根据流程实例id，获取当前Task任务id和名称
	 * @param processInstanceId
	 * @param executionId
	 * @return
	 */
	public Map<String,Object> getTaskIdAndNamesProcessInstanceId(String processInstanceId){
		Map<String,Object> mapResult = new HashMap<String, Object>();
		String taskIds = "";
		String taskNames = "";
		List<Task> lstTask = getTasksByProcessInstanceId(processInstanceId);
		if(!CollectionUtils.isEmpty(lstTask)) {
			for (Task task : lstTask) {
				if(StringUtils.isNotEmpty(taskIds)) taskIds += ",";
				taskIds += task.getId();
				
				if(StringUtils.isNotEmpty(taskNames)) taskNames += ",";
				taskNames += task.getName();
			}
		}
		
		mapResult.put("TASK_IDS", taskIds);
		mapResult.put("TASK_NAMES", taskNames);
		return mapResult;
	}
	
	/***
	 * 根据候选组ID查询拥有任务
	 * @param candidateGroups
	 * @return
	 */
	public List<Task> getTasksByCandidateGroups(String candidateGroups){
		return taskService.createTaskQuery().taskCandidateGroup(candidateGroups).list();
	}
	
	/***
	 * 根据候选组ID查询拥有任务
	 * @param lstCandidateGroups
	 * @return
	 */
	public List<Task> getTasksByCandidateGroups(List<String> lstCandidateGroups){
		return taskService.createTaskQuery().taskCandidateGroupIn(lstCandidateGroups).list();
	}
	
	/***
	 * 根据用户ID查询任务
	 * @param candidateUser
	 * @return
	 */
	public List<Task> getTasksByCandidateUser(String candidateUser){
		return taskService.createTaskQuery().taskCandidateUser(candidateUser).list();
	}
	
	/***
	 * 查询所拥有的个人&候选任务
	 * @param userIdForCandidateAndAssignee
	 * @return
	 */
	public List<Task> getTasksByUserIdForCandidateAndAssignee(String userIdForCandidateAndAssignee){
		return taskService.createTaskQuery().taskCandidateOrAssigned(userIdForCandidateAndAssignee).list();
	}
	
	/***
	 * 获取待办任务
	 * @param mapParams
	 * @return
	 */
	public List<Map<String,Object>> selectToDoTaskList(Map<String, Object> mapParams){
		return extActivitiTaskMapper.selectToDoTaskList(mapParams);
	}
	
	/***
	 * 完成任务
	 * @param taskId
	 * @param variables
	 */
	public void completeTask(String taskId,Map<String, Object> variables) {
		taskService.complete(taskId,variables);
	}
	
	/***
	 * 拾取任务并完成任务
	 * @param taskId
	 * @param userId
	 * @param variables
	 */
	public void completeCandidateTask(String taskId,String userId,Map<String, Object> variables) {
		taskService.claim(taskId, userId);
		taskService.complete(taskId,variables);
	}
	
	/***
	 * 根据任务id，获取批注内容
	 * @param taskId
	 * @return
	 */
	public List<Comment> getCommentsByTaskId(String taskId) {
		return taskService.getTaskComments(taskId);
	}
	
	/***
	 * 根据流程实例id，获取批注内容
	 * @param processInstanceId
	 * @return
	 */
	public List<Comment> getCommentsByProcessInstanceId(String processInstanceId) {
		return taskService.getProcessInstanceComments(processInstanceId);
	}
	
	/****
	 * 加入任务候选人
	 * @param taskId
	 * @param userId
	 */
	public void addCandidateUser(String taskId, String userId) {
		taskService.addCandidateUser(taskId,userId);
	}
	/****
	 * 加入任务的办理人
	 * 
	 * @param taskId
	 * @param userId
	 */
	public void setAssignee(String taskId, String userId) {
		taskService.setAssignee(taskId, userId);
	}

	/**
	 * 根据任务ID获得任务实例
	 * 
	 * @param taskId
	 *            任务ID
	 * @return
	 * @throws Exception
	 */
	public Task getTaskById(String taskId) throws Exception {
		Task task = (Task) taskService.createTaskQuery().taskId(taskId).singleResult();
		if (task == null) {
			throw new Exception("任务实例未找到!");
		}
		return task;
	}
}
